package com.flismax.mysms.activity.sms;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.flismax.mysms.R;
import com.flismax.mysms.activity.SendSmsActivity;
import com.flismax.mysms.sendsms.SmsSend;
import com.flismax.mysms.sms.control.Sms;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class QuanLySMS extends Activity{
	List<Sms> model=new ArrayList<Sms>();
	SMSAdapter adapter=null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listsms);
		ListView list=(ListView)findViewById(R.id.listsmsquanly);
	     adapter=new SMSAdapter();
	     list.setAdapter(adapter);
		Uri uriSMSURI = Uri.parse("content://sms/inbox");
	      Cursor cur = getContentResolver().query(uriSMSURI, null, null, null,null);
	      //String sms = "";
	      while (cur.moveToNext()) {
	    		  //sms += "From :" + cur.getString(2) + " : " + cur.getString(11)+ ":"+ts+"\n";
	    		  Sms s=new Sms();
		    	  s.setAddress(cur.getString(2));
		    	  s.setBody(cur.getString(11));
		    	  long ldate = cur.getLong(cur.getColumnIndex("date")); 
		    	  Date aDate = new Date(ldate); 
		    	  SimpleDateFormat formatter = new SimpleDateFormat( 
		                  "dd/MM/yyyy 'at' h:mm a");
		    	 // Integer aa=Integer.parseInt(aDate.toString());
		          long date =ldate;
		    	  s.setDate(date);
		    	  adapter.add(s);
	      }
	}
	//lam viec voi menu
	 public boolean onCreateOptionsMenu(Menu menu) {
		    new MenuInflater(this).inflate(R.menu.optionsms, menu);

		    return(super.onCreateOptionsMenu(menu));
		  }
	 @Override
	  public boolean onOptionsItemSelected(MenuItem item) {
	    if (item.getItemId()==R.id.searchsms) {
	      String message="selected search sms";
	      Toast.makeText(this, message, Toast.LENGTH_LONG).show();
	      Intent i = new Intent(this, SearchSMS.class);
         startActivity(i);
	      return(true);
	    }
	    else if (item.getItemId()==R.id.sendsms) {
	    	 String message="selected Send sms";
		     Toast.makeText(this, message, Toast.LENGTH_LONG).show();
		     Intent i = new Intent(this, SendSmsActivity.class);
		     startActivity(i);
	      return(true);
	    }
	    else if (item.getItemId()==R.id.deletesms) {
	    	 String message="selected deletesms";
		      Toast.makeText(this, message, Toast.LENGTH_LONG).show(); 
		      
		      return(true);
		    }
	    
	    return(super.onOptionsItemSelected(item));
	  }
	class SMSAdapter extends ArrayAdapter<Sms> {
		SMSAdapter() {
			super(QuanLySMS.this, R.layout.row, model);
		}
		public View getView(int position, View convertView,
                        ViewGroup parent) {
			View row=convertView;
			SMSHolder holder=null;
      
			if (row==null) {                          
				LayoutInflater inflater=getLayoutInflater();
				row=inflater.inflate(R.layout.row, parent, false);
				holder=new SMSHolder(row);
				row.setTag(holder);
			}
			else {
				holder=(SMSHolder)row.getTag();
			}
      
			holder.populateFrom(model.get(position));
      
			return(row);
		}
	}
	  
	  static class SMSHolder {
	    private TextView sdt=null;
	    private TextView noidung=null;
	    private ImageView icon=null;
	    
	    SMSHolder(View row) {
	      sdt=(TextView)row.findViewById(R.id.sdt);
	      noidung=(TextView)row.findViewById(R.id.noidung);
	      icon=(ImageView)row.findViewById(R.id.icon);
	    }
	    
	    void populateFrom(Sms r) {
	      sdt.setText(r.getAddress());
	      noidung.setText(r.getBody()+" "+r.getDate());
	      int i=(int) (Math.random()*3);
	      if (i==0) {
	        icon.setImageResource(R.drawable.ball_red);
	      }
	      else if (i==2) {
	        icon.setImageResource(R.drawable.ball_yellow);
	      }
	      else {
	        icon.setImageResource(R.drawable.ball_green);
	      }
	    }
	  }

}
